package cn.edu.zut.hkxy.service.impl;

import cn.edu.zut.hkxy.mapper.DownloadsMapper;
import cn.edu.zut.hkxy.mapper.FtpMapper;
import cn.edu.zut.hkxy.pojo.Downloads;
import cn.edu.zut.hkxy.pojo.DownloadsExample;
import cn.edu.zut.hkxy.pojo.Ftp;
import cn.edu.zut.hkxy.pojo.FtpExample;
import cn.edu.zut.hkxy.service.DocsService;
import cn.edu.zut.hkxy.service.DownloadsService;
import cn.edu.zut.hkxy.util.DateFormat;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class DownloadsServiceImpl implements DownloadsService {
    @Autowired
    private DownloadsMapper downloadsMapper;
    @Autowired
    private DocsService docsService;
    @Autowired
    private FtpMapper ftpMapper;

    @Override
    public int add(Downloads downloads) {
        Date date=new Date();
        downloads.setDate(date);
        downloads.setTime(date);
        downloads.setDatetime(DateFormat.formatToString(date));
        int download_id=downloadsMapper.insertGetId(downloads);
        return download_id;
    }

    @Override
    public boolean delete(Integer id) {
        int i=downloadsMapper.deleteByPrimaryKey(id);
        docsService.deleteDownloadDocsBatch(id);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Downloads> getPageList(DownloadsExample example, int pageNum) {
        example.setOrderByClause("datetime desc");
        //分页：第pageNum页，每页10条数据
        PageHelper.startPage(pageNum,10);
        List<Downloads> list=downloadsMapper.selectByExample(example);
        return list;
    }

    @Override
    public Downloads getWithDocsById(Integer id) {
        return downloadsMapper.selectWithDocsById(id);
    }


    //以下操作FTP
    @Override
    public boolean deleteOneFtp(Integer id) {
        int i=ftpMapper.deleteByPrimaryKey(id);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean modifyOneFtp(Ftp ftp) {
        int i=ftpMapper.updateByPrimaryKeySelective(ftp);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean addOneFtp(Ftp ftp) {
        int i=ftpMapper.insert(ftp);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Ftp> getFtpList() {
        FtpExample example=new FtpExample();
        List<Ftp> ftpList=ftpMapper.selectByExample(example);
        return ftpList;
    }
}
